package com.extension.company.common.widget.popuwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.extension.company.admin.R;

/**
 * 作者:zwk
 * 联系方式：497981239@qq.com
 * 时间：on 2019/7/2 16:09
 * 功能：退款原因的popwindow
 */
public class RefundReasonPW extends PopupWindow implements View.OnClickListener {
	private Activity context;
	private Button close;
	private TextView reason;
	private LinearLayout firstItem;

	public RefundReasonPW(Activity context) {
		this.context = context;
		View view = LayoutInflater.from(context).inflate(R.layout.refund_reason_popwindow, null);
		initView(view);
		initOnclick();
		this.setContentView(view); // 设置popwindow的界面
		this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
		this.setFocusable(true);
//		this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0x00000000); //半透明色
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.popwin_anim_style); // pop进入退出的动画
		this.setOutsideTouchable(true);// 点击pop外，可以关闭

		view.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int top = firstItem.getTop();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					int y = (int) event.getY(); // 提起时，Y的值
					if (y < top) {
						dismiss();
					}
				}

				return true;
			}
		});

	}


	/**
	 * @param view 布局
	 */
	private void initView(View view) {
		close = view.findViewById(R.id.close);
		reason = view.findViewById(R.id.reason);
		firstItem = view.findViewById(R.id.pop_first_item);
		this.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				backgroundAlpha(1f);
			}
		});
	}

	/**
	 * 点击监听事件
	 */
	private void initOnclick() {
		close.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.close:
				if (null != closeListener) {
					closeListener.close();
				}
				break;
			default:
				break;
		}
	}

	public void show() {
		if (!isShowing()) {
			showAtLocation(((ViewGroup) context.findViewById(android.R.id
					.content)).getChildAt(0), Gravity.CENTER, 0, 0); // 在布局中第一个viewGroup
			backgroundAlpha(0.7f);
		} else {
			dismiss();
		}
	}

	public void showPopupWindow(View parent) {
		if (!isShowing()) {
			showAsDropDown(parent, parent.getLayoutParams().width / 2, 20);
		} else {
			dismiss();
		}
	}


	private void backgroundAlpha(float bgAlpha) {
		if (bgAlpha == 1) {
			//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
			context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		} else {
			//此行代码主要是解决在华为手机上半透明效果无效的bug
			context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0~1.0
		context.getWindow().setAttributes(lp); //act 是上下文context
	}

	public void hideSoftInputView() {
		InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (((Activity) context).getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
			if (((Activity) context).getCurrentFocus() != null) {
				manager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	public interface closeListener {
		void close();
	}

	private closeListener closeListener;

	public void setCloseListener(closeListener closeListener) {
		this.closeListener = closeListener;
	}
}
